package com.qf.utils.http.httpclient;

import com.qf.utils.SmallTool;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCookieStore;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author claud
 * 2018年8月15日17:47:52
 * httpclient的所需参数类
 */
@Data
public class HttpClientConfig {


    //"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36";
    public static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36";
    /**
     * 默认的超时时间--wos的一般时间都很长，需要设置为25000
     */
    public static int TIME_OUT = 10000;
    private static Integer bdIpIndex = 0;
    /**
     * 请求配置
     */
    private RequestConfig requestConfig;
    /**
     * 代理请求配置
     */
    private CredentialsProvider credsProvider;
    private Map<String, String> headerMap = new HashMap<>();
    private String refer;
    private String host;
    private String proxyIp = "127.0.0.1";
    private Integer proxyPort = 11223;
    // 开启外网代理
    private boolean isProxy = true;
    private Boolean sslUse = false;
    /**
     * 存储空间路径
     */
    private String storagePath;
    //存储路径
    private String dirPath = UrlEntity.PathHead + "\\temp\\temp\\";
    //存储文件名
    private String filePath;
    private BasicCookieStore cookieStore = new BasicCookieStore();
    /**
     * 特殊cookie
     */
    private String cookie;
    /**
     * 需要跳转的地址
     */
    private String url;


    public HttpClientConfig() {
        super();
        this.requestConfig = createRequestConfigure(null, null, null, TIME_OUT, true);
    }

    /**
     * 生成代理ip的请求配置的方法
     * ConnectionTimeout是tcp连接建立的时间--即tcp三次握手完成时间.
     * SocketTimeout是连接建立后,数据传输过程中数据报之间间隔的最大时间.
     * localAdd是多ip的主机的不同出口ip
     */
    public static RequestConfig createRequestConfigure(String remoteIp, Integer remotePort, String localAdd, int timeOut, Boolean redirectFlag) {
        HttpHost host = null;
        if (!StringUtils.isEmpty(remoteIp)) {
            host = new HttpHost(remoteIp, remotePort);
        }
        InetAddress adds = null;
        if (localAdd != null) {
            try {
                adds = InetAddress.getByName(localAdd);
            } catch (UnknownHostException e) {
                SmallTool.printMessError("异常信息++" + e.getMessage());
            }
        }
        RequestConfig requestConfig = RequestConfig.custom()
//                .setCookieSpec(CookieSpecs.STANDARD)
                .setSocketTimeout(timeOut)
                .setConnectTimeout(timeOut)
                .setRedirectsEnabled(redirectFlag)
                .setProxy(host)
                .setLocalAddress(adds)
                .build();
        return requestConfig;
    }

    public boolean isProxy() {
        return isProxy;
    }

    public void setProxy(boolean proxy) {
        isProxy = proxy;
    }


}
